package com.pay.game.central.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pay.game.central.common.Result;
import com.pay.game.central.common.ResultGenerator;
import com.pay.game.central.security.annotaion.PassToken;
import com.pay.game.central.security.annotaion.UserLoginToken;
import com.pay.game.central.web.bean.PddGoods;
import com.pay.game.central.web.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("goods")
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * @return index=1 的商品
     */
    @GetMapping("/firstId")
    @ResponseBody
    Result<String> getFirstId(){
        return ResultGenerator.genSuccessResult(goodsService.getFirstId());
    }

    @PassToken
    @PostMapping("/list")
    @ResponseBody
    Result<String> addList(@RequestBody String body) {
        List<PddGoods> list = JSON.parseArray(body, PddGoods.class);
        for (PddGoods item: list) {
            if (StringUtils.isEmpty(item.getGroup_id()) || StringUtils.isEmpty(item.getSku_id())){
                return ResultGenerator.genFailResult("商品(" + item.getId() + ")信息上传不完整。");
            }
        }
        list.forEach(item -> {
            item.setUpdate_time(System.currentTimeMillis());
            goodsService.addOrUpdateGoods(item);
        });
        return ResultGenerator.genSuccessResult("操作成功");
    }


    @UserLoginToken
    @GetMapping("/list")
    @ResponseBody
    Result<PageInfo<Map<String,Object>>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(defaultValue = "0") float price,
            String id, String good_name, String user_name,String store_id, String store_name){
        PageHelper.startPage(page, limit);
        return ResultGenerator.genSuccessResult(new PageInfo<>(goodsService.list(id, store_id, store_name, (int) price,good_name,user_name)));
    }

    @UserLoginToken
    @PostMapping("{id}/loop")
    @ResponseBody
    Result<String> loop(@PathVariable("id") String id,boolean loop){
        goodsService.setLoop(id,loop);
        return ResultGenerator.genSuccessResult("设置成功");
    }

    /**
     * 爬虫-> skuId,groupId（获取下一条商品）
     * @param currentId
     * @return
     */
    @GetMapping("/nextId")
    @ResponseBody
    Result<String> getNextId(String currentId){
        return ResultGenerator.genSuccessResult(goodsService.getNextId(currentId));
    }

    /**
     * 设置商品 skuId,groupId
     * @param body {id,skuId,groupId}
     * @return
     */
    @PostMapping("/setSkuIdAndGroup")
    @ResponseBody
    Result<Boolean> setSkuIdAndGroupAndPrice(@RequestBody String body){
        JSONObject jsonObject = JSON.parseObject(body);
        goodsService.setGoodsSkuAndGroupAndPrice(
                jsonObject.getString("id"),
                jsonObject.getString("skuId") ,
                jsonObject.getString("groupId"),
                jsonObject.getInteger("price"));
        return ResultGenerator.genSuccessResult(true);
    }

    /**
     *
     * @return  所有商品
     */
    @GetMapping("/all")
    @ResponseBody
    Result<List<PddGoods>> all(){
        return ResultGenerator.genSuccessResult(goodsService.getAllGoodsWithStoreName());
    }


    /**
     * 指定价格商品集合
     * @param price price
     * @return
     */
    @GetMapping("/list/price")
    @ResponseBody
    Result<List<PddGoods>> getGoodsByPrice(float price){
        return ResultGenerator.genSuccessResult(goodsService.getGoodsByPrice((int) (price*100)));
    }

    @PostMapping("/{id}/groupOrderId")
    @ResponseBody
    Result<Boolean> setGroupOrderId(@PathVariable String id,@RequestBody String body){
        JSONObject jsonObject = JSON.parseObject(body);
        String groupOrderId = jsonObject.getString("groupOrderId");
        goodsService.setGroupOrderId(id, groupOrderId);
        return ResultGenerator.genSuccessResult(true);
    }


}